package com.qfedu.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.config.RocketmqConfig;
import com.qfedu.params.AddVideo;
import com.qfedu.provider.entity.DbVideo;
import com.qfedu.provider.mapper.DbVideoMapper;
import com.qfedu.provider.service.DbVideoService;
import com.qfedu.utils.R;
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【db_video(短视频)】的数据库操作Service实现
* @createDate 2024-11-23 11:16:39
*/
@Service
@RequiredArgsConstructor
public class DbVideoServiceImpl extends ServiceImpl<DbVideoMapper, DbVideo>
    implements DbVideoService {
    private final DbVideoMapper dbVideoMapper;
    private final RocketMQTemplate rocketMQTemplate;
    @Override
    public R publish(AddVideo addVideo,Integer uid) {
        DbVideo dbVideo = new DbVideo();
        BeanUtils.copyProperties(addVideo,dbVideo);
        dbVideo.setUserId(uid.toString());
        dbVideo.setStatus(1);
        dbVideo.setCommintNumber(0);
        dbVideo.setLikeNumber(0);
        dbVideo.setDeleted(1);
        dbVideo.setCreateTime(new Date());
        dbVideo.setUpdateTime(new Date());
        dbVideo.setCollectionNumber(0);
        dbVideoMapper.insert(dbVideo);
        System.out.println("dbVideo内容为"+dbVideo);
        rocketMQTemplate.convertAndSend(RocketmqConfig.TOPIC_CONTENT,dbVideo.getId());
        return R.ok("上传成功,等待审核");
    }

    @Override
    public List<DbVideo> findByUserId(Integer userId) {
        LambdaQueryWrapper<DbVideo> lambda=new LambdaQueryWrapper<>();
        lambda.eq(DbVideo::getUserId, userId);
        lambda.eq(DbVideo::getStatus, 2);
        return dbVideoMapper.selectList(lambda);
    }

    /**
     * 根据类型查询视频
     * @param typeId
     * @return
     */
    @Override
    public R queryAllVideoByType(Integer typeId) {
        LambdaQueryWrapper<DbVideo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DbVideo::getTypeId,typeId);
        wrapper.eq(DbVideo::getStatus,2);
        wrapper.eq(DbVideo::getDeleted,1);
        List<DbVideo> list = dbVideoMapper.selectList(wrapper);
        if (list.size()==0){
            return R.error("不存在该类型视频");
        }
        return R.ok(list);
    }


    @Override
    public List<DbVideo> findAll() {
        return dbVideoMapper.selectList(new LambdaQueryWrapper<DbVideo>().eq(DbVideo::getStatus, 2));
    }
    @Override
    public R findByTypeId(Integer typeId) {
        List<DbVideo> list=dbVideoMapper.selectList(new LambdaQueryWrapper<DbVideo>().eq(DbVideo::getTypeId, typeId).eq(DbVideo::getStatus, 2));
        return R.ok(list);
    }

    @Override
    public R deleteById(Integer videoId) {
        int i = dbVideoMapper.deleteById(videoId);
        if (i>0){
            return R.ok("删除成功");
        }
        return R.error("视频不存在");
    }
}